package com.tr.rentcar.controller.goods;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.tr.rentcar.common.BaseServlet;
import com.tr.rentcar.model.bean.Cars;
import com.tr.rentcar.model.dao.CarInfoDao;
import com.tr.rentcar.model.jdbc.MySqlDb;

@WebServlet("/show")
public class GoodsShowController extends HttpServlet {
	
	/**
	 * 商城页面通过ajax传值
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json;charset=utf-8");
		PrintWriter out = resp.getWriter();
		CarInfoDao cid=new CarInfoDao();
		String style=req.getParameter("style");
		String brand=req.getParameter("brand");
		String seq=req.getParameter("seq");
		int pageNo=Integer.parseInt(req.getParameter("pageNo"));
		try {
			String[] price=req.getParameter("price").split("-");
			int pageSize=5;//当前页显示记录数
			if("全部".equals(price[0])){
				List<Cars> list=cid.getByCondition(style, brand, "全部", "全部",seq,(pageNo-1)*pageSize,pageSize);
				//System.out.println(JSONArray.toJSONString(list));
				out.print(JSONArray.toJSONString(list));
			}else{
				List<Cars> list=cid.getByCondition(style, brand, price[0], price[1],seq,(pageNo-1)*pageSize,pageSize);
				out.print(JSONArray.toJSONString(list));
			}
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
	}
	/**
	 * 传记录数
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		PrintWriter out = resp.getWriter();
		CarInfoDao cid=new CarInfoDao();
		String style=req.getParameter("style");
		String brand=req.getParameter("brand");
		String seq=req.getParameter("seq");
		try {
			String[] price=req.getParameter("price").split("-");
			if("全部".equals(price[0])){
				long count=cid.getByConditionCount(style, brand, "全部", "全部", seq);
				out.print(count);
			}else{
				long count=cid.getByConditionCount(style, brand, price[0], price[1], seq);
				out.print(count);
			}
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
	}

}
